package model

import (
	"database/sql"
	"github.com/gomodule/redigo/redis"
	"strconv"
	"time"
	"tizhi_front/nspel"
)

type School struct {
	Id               int64           `gorm:"AUTO_INCREMENT;primary_key;column:id"`
	CountryId        int64           `gorm:"column:country_id"`
	ProvinceId       int64           `gorm:"column:province_id"`
	CityId           int64           `gorm:"column:city_id"`
	AreaId           int64           `gorm:"column:area_id"`
	ExperimentAreaId int64           `gorm:"column:experiment_area_id"`
	UserId           sql.NullInt64   `gorm:"column:user_id"`
	Name             string          `gorm:"column:name"`
	SchoolCode       string          `gorm:"column:school_code"`
	Code             sql.NullString  `gorm:"column:code"`
	StudentNum       int64           `gorm:"column:student_num"`
	SchoolType       byte            `gorm:"column:school_type"`
	HeadMaster       sql.NullString  `gorm:"column:headmaster"`
	HeadMasterPhone  sql.NullString  `gorm:"column:headmaster_phone"`
	HeadMasterEmail  sql.NullString  `gorm:"column:headmaster_email"`
	Contact          sql.NullString  `gorm:"column:contact"`
	ContactPhone     sql.NullString  `gorm:"column:contact_phone"`
	ContactEmail     sql.NullString  `gorm:"column:contact_email"`
	ContactAddress   sql.NullString  `gorm:"column:contact_address"`
	HasApply         bool            `gorm:"column:has_apply"`
	Homepage         sql.NullString  `gorm:"column:homepage"`
	OfficialAccounts sql.NullString  `gorm:"column:official_accounts"`
	IsExperiment     bool            `gorm:"column:is_experiment"`
	IsStandout       bool            `gorm:"column:is_standout"`
	ExperimentAt     time.Time       `gorm:"column:experiment_at"`
	StandoutAt       time.Time       `gorm:"column:standout_at"`
	JoinAt           time.Time       `gorm:"column:join_at"`
	Score            sql.NullFloat64 `gorm:"column:score"`
	CreatedAt        time.Time       `gorm:"column:created_at"`
	UpdatedAt        time.Time       `gorm:"column:updated_at"`
}

func (*School) TableName() string {
	return "school"
}

type School0 struct {
	SchoolType byte   `gorm:"column:school_type"`
	Name       string `gorm:"column:name"`
}

func (*School0) TableName() string {
	return "school"
}

func (s *School0) SelectById(id int64) (err error) {
	key := "school0.id:" + strconv.FormatInt(id, 10)
	err = nspel.RedisGetGob(key, s)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"school_type", "name"}).Where("id = ?", id).First(s).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, s)
	return
}
